package com.finalproject.ui;
/**
 * UiCafeTabPanel repesentation of Cafe tab
 */
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

import com.finalproject.entity.Cafe;
import com.finalproject.entity.CafeDatabase;
import com.finalproject.entity.CardHolder;
import com.finalproject.entity.CardHolderDatabase;
import com.finalproject.entity.CardHolderDatabase.CardHolderDatabaseObserver;

public class UICafeTabPanel extends JPanel implements CardHolderDatabaseObserver{
	
	
	
	private UICardHolder uiCardHolder;
	
	private JPanel uiCafePanel;
	
	private UIMap uiMapPanel;
	
	
	private JPanel leftPanel;
	private JPanel rightPanel;
	
	private UICafe uiCafe;
	private CardHolder cardHolder;
	
	
	private CafeDatabase cafeDatabase = CafeDatabase.getInstance();
	
	public UICafeTabPanel() {
		CardHolderDatabase.getInstance().addCardHolderDatabaseObserver(this);
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createTitledBorder("Cafes"));
		this.uiCafePanel = new JPanel();
		leftPanel = new JPanel();
		
		rightPanel = new JPanel();
		rightPanel.setLayout(new BorderLayout());
		
		uiCardHolder = new UICardHolder();
		uiCardHolder.setBorder(BorderFactory.createTitledBorder("CardHolder Panel"));
	
		add(leftPanel, BorderLayout.WEST);
		add(rightPanel, BorderLayout.EAST);
		
		leftPanel.setLayout(new BorderLayout());
		
		leftPanel.add(uiCafePanel, BorderLayout.CENTER);
		
		
		
		this.uiMapPanel = createMap();
		uiMapPanel.setLayout(new FlowLayout());
		uiMapPanel.setPreferredSize(new Dimension(100,400));
		rightPanel.add(uiCardHolder,BorderLayout.NORTH);
		rightPanel.add(uiMapPanel,BorderLayout.CENTER);
		
		
		
		
		
		rightPanel.setPreferredSize(UIConstants.RIGHT_PANEL_PREFERRED);
		uiMapPanel.setLayout(null);
		
	}
	
	
	private UIMap createMap() {
		UIMap uiMap = new UIMap();
		List<Cafe> cafes = cafeDatabase.getCafes();		
		
		if(!cafes.isEmpty()) {
			Cafe cafe = cafes.get(0);
			showCafe(cafe);
		}
		
		
		for(int i = 0; i < cafes.size(); i++) {
			final Cafe cafe =  cafes.get(i);
			int[] coordinates = cafe.getCoordinates();
			String mapLabel = "C#" + cafe.getId();
			JButton mapButton = new JButton(mapLabel);
		
			uiMap.addLocation(mapButton, coordinates);
			mapButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					showCafe(cafe);
				}
				
			});
		}
		return uiMap;
	}
	
	
	private void showCafe(Cafe cafe) {
		uiCafePanel.removeAll();
		uiCafe = new UICafe(cafe);
		uiCafe.setCardHolder(cardHolder);
		uiCafePanel.add(uiCafe);
		uiCafePanel.revalidate();
		
		
	}


	@Override
	public void onLogin(CardHolder cardHolder, Integer familyId) {
		this.cardHolder = cardHolder;
		uiCafe.setCardHolder(cardHolder);
		
	}


	@Override
	public void onLogout(CardHolder cardHolder, Integer familyId) {
		// TODO Auto-generated method stub
		
		this.cardHolder = null;
		uiCafe.setCardHolder(null);
	}

}
